package databasetranslation;

import static org.junit.Assert.assertEquals;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import models.Client;
import models.Subscription;
import models.Workshop;
import models.Workshop.DagDeel;

import org.junit.Before;
import org.junit.Test;

import database.DummySQLServer;
import database.SQLServer;

/**
 * Tests the subscription list with a SQL server.
 *
 */
public class testSubscriptionList_NoDB {
	protected SubscriptionList list;
	protected SubscriptionList newList;
	protected Subscription subscription1;
	protected Subscription subscription2;
	
	protected SQLServer buildServer() {
		return new DummySQLServer();
	}
	
	@Before
	public void setUp() throws SQLException {
		Date now = new Date();
		models.Workshop.DagDeel dagDeel = DagDeel.VOORMIDDAG;
		
		//setup clientList
		Client client1 = new Client("Henk", "Vries", "Myroad 15, 28240 MyCity", "015 928749", "h.vries@gmail.com");
		Client client2 = new Client("Joop", "Spencer", "Myroad 16, 28240 MyCity", "06 394", "j.spencer@gmail.com");
		ClientList clientList = new ClientList(buildServer());
		clientList.add(client1);
		clientList.add(client2);
		
		//setup workshopList
		Workshop workshop1 = new Workshop("title1", now, dagDeel, 10.0, "explanation", 1, "docent1");
		Workshop workshop2 = new Workshop("title2", now, dagDeel, 10.0, "explanation", 1, "docent2");
		WorkshopList workshopList = new WorkshopList(buildServer());
		workshopList.add(workshop1);
		workshopList.add(workshop2);
		
		//setup subscriptionList
		list = new SubscriptionList(buildServer(), workshopList, clientList);
		newList = new SubscriptionList(buildServer(), workshopList, clientList);
		list.clearDatabaseTable();
		
		subscription1 = new Subscription(client1,workshop1);
		subscription2 = new Subscription(client2,workshop2);
	}
	
	@Test
	public void test_getSubscriptions() throws SQLException {
		list.add(subscription1);
		list.add(subscription2);
		Subscription[] expected = {subscription1, subscription2};
		assertEquals(list.getSubscriptions(), Arrays.asList(expected));
	}
	
	@Test
	public void testSubscriptionListAdd() throws SQLException {
		list.add(subscription1);
		List<Subscription> testlist = new ArrayList<Subscription>();
		testlist.add(subscription1);
		assertEquals(testlist,list.getSubscriptions());
	}
	
	@Test
	public void testSubscriptionListDelete() throws SQLException {
		list.add(subscription1);
		list.add(subscription2);
		list.delete(subscription2);
		List<Subscription> testlist = new ArrayList<Subscription>();
		testlist.add(subscription1);
		assertEquals(testlist,list.getSubscriptions());
	}	
	
	@Test
	public void testSubscriptionListReplace() throws SQLException {	
		list.add(subscription2);
		subscription2.setClient(subscription1.getClient());
		list.replace(subscription2);
		List<Subscription> testlist = new ArrayList<Subscription>();
		testlist.add(subscription2);
		assertEquals(testlist,list.getSubscriptions());
	}

	@Test
	public void testIterItems() throws SQLException {
		list.add(subscription1);
		List<Subscription> testList = new ArrayList<Subscription>();
		testList.add(subscription1);
		assertEquals(testList, list.getEntries());
	}
	
	@Test
	public void testGetSize() {
		assertEquals(0, list.getSize());
	}
	
}